#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2019/12/13 09:03|09:03
# @Author  : yangdingyi
# @File    : 两个数组的交集 II
# @Software: PyCharm
from typing import List


class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        map1 = {}
        map2 = {}
        ans = []
        for i, a in enumerate(nums1):
            v = map1.get(a)
            map1[a] = (v if v else 0) + 1
        for i, a in enumerate(nums2):
            v = map2.get(a)
            map2[a] = (v if v else 0) + 1
        for k, v1 in map1.items():
            v2 = map2.get(k)
            if v1 > 0 and v2:
                v = v1 if v1 <= v2 else v2
            else:
                v = 0
            while v > 0:
                ans.append(k)
                v -= 1
        return ans


solve = Solution()

nums1 = [1, 2, 2, 1]
nums2 = [2, 2]
print(nums1, nums2)
print(solve.intersect(nums1, nums2))


